<template>
	<view>
		<FullscreenLoading :loading="loading" :config="config"></FullscreenLoading>
		<view class="muu-scoreshop" :class="[config.style]">
			<view class="main-section tabbar-bottom" v-if="!loading">
				<view class="item" v-for="(item,index) in pageData.data" :key="index">
					<view v-if="item.type==='announce'">
						<Announce title="公告组件" type="announce" :params="item.data" :config="config" />
					</view>
					<view v-if="item.type==='category'">
						<Category title="分类组件" type="category" :params="item.data" :config="config" />
					</view>
					<view v-if="item.type==='category_nav'">
						<CategoryNav title="图文导航组件" type="category_nav" :params="item.data" :config="config" />
					</view>
					<view v-if="item.type==='search'">
						<Search title="搜索组件" type="search" :config="config" />
					</view>
					<view v-if="item.type==='slideshow'">
						<SlideShow title="轮播组件" type="slideshow" :slideshowStyle="Number(item.style)" :params="item.data" :config="config" />
					</view>
					<view v-if="item.type==='single_img'">
						<SingleImg title="单图广告组件" type="single_img" :singleImgStyle="Number(item.style)" :params="item.data" :config="config"/>
					</view>
					<view v-if="item.type==='weixin'">
						<Weixin title="关注公众号组件" type="weixin" :thisStyle="Number(item.style)" :config="config" />
					</view>
					<view v-if="item.type==='goods_list'">
						<ScoreshopGoodsList title="积分商城商品列表组件" type="scoreshop_goods_list" :params="item.data" :config="config" />
					</view>
					<view v-if="item.type==='activity_list'">
						<ActivityList title="线下活动列表组件" type="activity_list" :params="item.data" :config="config" />
					</view>
					<view v-if="item.type==='minishop_goods_list'">
						<MinishopGoodsList title="云小店商品列表组件" type="scoreshop_goods_list" :params="item.data" :config="config" />
					</view>
				</view>
				<template v-if="!loading">
					<CopyRight :data="config.copyright" />
				</template>
			</view>
			<tabBar v-if="!loading" />
		</view>
	</view>
</template>

<script>
	import { mapState,mapMutations,mapActions } from 'vuex'
	//DIY组件 按首字母顺序排列
	import ActivityList from '@/common/diys/ActivityList/ActivityList.vue' //线下活动组件
	import Announce from '@/common/diys/Announce/Announce.vue' //公告组件
	import Category from '@/common/diys/Category/Category.vue' //分类组件
	import CategoryNav from '@/common/diys/CategoryNav/CategoryNav.vue' //图文导航组件
	import MinishopGoodsList from '@/common/diys/MinishopGoodsList/MinishopGoodsList.vue' //云小店商品列表组件
	import ScoreshopGoodsList from '@/common/diys/ScoreshopGoodsList/ScoreshopGoodsList.vue' //积分商城商品列表组件
	import Search from '@/common/diys/Search/Search.vue' //搜索组件
	import SlideShow from '@/common/diys/SlideShow/SlideShow.vue' //轮播组件
	import SingleImg from '@/common/diys/SingleImg/SingleImg.vue' //单图广告组件
	import Weixin from '@/common/diys/Weixin/Weixin.vue' //关注公众号组件
	//应用组件
	import CopyRight from '@/common/components/copyright.vue' //自定义版权
	import TabBar from "../common/tabBar.vue" //底部导航

	export default {
		name: 'index',
		components: {
			ActivityList,
			Announce,
			Category,
			CategoryNav,
			MinishopGoodsList,
			ScoreshopGoodsList,
			Search,
			SlideShow,
			SingleImg,
			Weixin,
			CopyRight,
			TabBar,
		},
		data() {
			return {
				loading: true,
				urlParams: {},
				userInfo : {}, //用户数据
				pageData : {}, //首页数据
			}
		},
		onReady() {

		},
		onPageScroll(e) {
			
		},
		onPullDownRefresh() {
			console.log('refresh');
			uni.removeStorageSync('muu_scoreshop_config')
			uni.removeStorageSync('muu_scoreshop_page_data')
			// 重新获取页面数据
			this.getApiPageData()
			// 更新全局配置
			this.$store.dispatch('setConfigAsync').then(res => {
				console.log(res)
			})
		},
		async onLoad(option) {
			await this.$onLaunched;
			this.urlParams = option
			//await this.setConfigAsync()
			await this.getPageData()
			await this.getUserInfo()
			await this.initPage(this.config)
			await this.$nextTick(() => {
				this.loading = false
			})
		},
		created() {
		},
		
		computed: {
			...mapState(['config'])
		},
		methods: {
			...mapActions([
				'setConfigAsync'
			]),
			
			initPage(config) {
				//设置原生导航栏
				this.setNavigation(this.config.style);
				//设置原生导航栏标题
				uni.setNavigationBarTitle({
				　　title:this.config.title
				})
				// H5端执行
				//#ifdef H5
				let url = window.location.href
				let shareData = {
					title : this.config.share.title,
					desc : this.config.share.desc,
					img: this.config.share.icon
				}
				this.wechat.share(shareData,url);
				//#endif
			},
			/**
			 * 获取页面数据
			 */
			getPageData() {
				let that = this
				return new Promise((resolve, reject) => {
					const page_data = uni.getStorageSync('muu_scoreshop_page_data');
					if(page_data.deadtime){
						let newtime = Date.parse(new Date());
						if(page_data.deadtime < newtime){
							resolve(that.getApiPageData())
						}else{
							that.pageData = page_data.data
							resolve(page_data)
						}
					}else{
						resolve(that.getApiPageData())
					}
				})
			},
			/**
			 * 获取接口数据
			 */
			getApiPageData: function(){
				let that = this
				
				let params = this.urlParams
				//console.log(params)
				let data = {}
					data.type = 1
					if(params.hasOwnProperty("promoter_uid") && params.promoter_uid !== undefined){
						data.promoter_uid = params.promoter_uid
					}
				let url = '';
					//#ifdef MP-WEIXIN
					url = 'entry/wxapp/index'
					//#endif
					//#ifdef H5
					url = 'entry/site/index'
					//#endif
				//获取首页数据
				this.util.request({
					url : url,
					data : data,
					success: function (res) {
						if (res.data.code == 200) {
							that.pageData = res.data.data
							// 缓存试卷 一个小时
							let deadtime = Date.parse(new Date()) + 3600 * 1000
							let page_data_store = {
								'deadtime' : deadtime,
								'data' : res.data.data
							}
							//设置本地缓存
							uni.setStorageSync('muu_scoreshop_page_data',page_data_store);
							//结束下拉刷新
							uni.stopPullDownRefresh();
						}
					},
				})
			},
			
			/**
			 * 获取已登录用户数据
			 */
			getUserInfo: function(){
				let that = this
				return new Promise((resolve, reject) => {
					let userInfo = uni.getStorageSync('userInfo') || {}
					if(userInfo){
						this.userInfo = userInfo
						resolve(userInfo)
					}else{
						this.util.getUserInfo((res)=>{
							console.log('userInfo',res)
							this.userInfo = res
							resolve(res)
						})
					}
				})
			},
			
			setNavigation: function(style) {
				let that = this
				//动态更改顶部状态栏演示
				let backgroundColor = '#ffffff'
				//console.log(this.config)
				if(style == 'Green'){
					backgroundColor = '#00ce74';
				}
				if(style == 'Blue'){
					backgroundColor = '#03b8cf';
				}
				if(style == 'LightRed'){
					backgroundColor = '#ea644a';
				}
				if(style == 'Orange'){
					backgroundColor = '#ff9900';
				}
				if(style == 'LightPink'){
					backgroundColor = '#ffb6c1';
				}
				if(style == 'Magenta'){
					backgroundColor = '#8666b8';
				}
				console.log(backgroundColor);
				//更改小程序原生导航栏
				uni.setNavigationBarColor({
					frontColor: '#ffffff',
					backgroundColor: backgroundColor,
				})
			}
		},
		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage() {
			// 分享(分销)用户ID
			let promoterUid = this.userInfo.memberInfo.uid || 0
			let share = {
				title : this.config.title,
				path : '@/muu_minshop/pages/index/index?' + '&promoter_uid=' + promoterUid,
				desc : this.config.description,
				success : function(){
					
				},
			}
			
			return share
		},
		/**
		 * 监听用户点击右上角转发到朋友圈
		 */
		onShareTimeline() {
			
		}
	}
</script>

<style lang="scss">

</style>
